package com.zhuhjay.model.domain;

import cn.hutool.system.JvmInfo;
import cn.hutool.system.OsInfo;
import cn.hutool.system.SystemUtil;
import cn.hutool.system.oshi.CpuInfo;
import cn.hutool.system.oshi.OshiUtil;
import com.zhuhjay.model.domain.server.*;
import com.zhuhjay.model.domain.server.System;
import lombok.Data;
import oshi.hardware.GlobalMemory;
import oshi.software.os.OSFileStore;

import java.util.LinkedList;
import java.util.List;

/**
 * @author ZhuHJay
 * @date 2023/2/12 22:16
 */
@Data
public class Server {

    /** CPU相关信息 */
    private Cpu cpu = new Cpu();

    /** 內存相关信息 */
    private Memory memory = new Memory();

    /** JVM相关信息 */
    private Jvm jvm = new Jvm();

    /** 服务器相关信息 */
    private System system = new System();

    /** 磁盘相关信息 */
    private List<SystemFile> systemFiles = new LinkedList<>();

    public Server() {
        setCpuInfo();
        setMemoryInfo();
        setJvmInfo();
        setSystemInfo();
        setSystemFiles();
    }

    /** 设置CPU信息 */
    private void setCpuInfo() {
        // CPU信息
        CpuInfo cpuInfo = OshiUtil.getCpuInfo(500);
        cpu.setCpuNum(cpuInfo.getCpuNum());
        cpu.setTotal(cpuInfo.getToTal());
        cpu.setSys(cpuInfo.getSys());
        cpu.setUsed(cpuInfo.getUsed());
        cpu.setWait(cpuInfo.getWait());
        cpu.setFree(cpuInfo.getFree());
    }

    /** 设置内存信息 */
    private void setMemoryInfo() {
        GlobalMemory globalMemory = OshiUtil.getMemory();
        memory.setTotal(convertSize(globalMemory.getTotal()));
        memory.setUsed(convertSize(globalMemory.getTotal() - globalMemory.getAvailable()));
        memory.setFree(convertSize(globalMemory.getAvailable()));
    }

    /** 设置服务器信息 */
    private void setSystemInfo() {
        OsInfo osInfo = SystemUtil.getOsInfo();
        system.setComputerName(SystemUtil.getHostInfo().getName());
        system.setComputerIp(SystemUtil.getHostInfo().getAddress());
        system.setOsName(osInfo.getName());
        system.setOsArch(osInfo.getArch());
        system.setUserDir(SystemUtil.getUserInfo().getCurrentDir());
    }

    /** 设置Java虚拟机 */
    private void setJvmInfo() {
        JvmInfo jvmInfo = SystemUtil.getJvmInfo();
        jvm.setTotal(convertSize(SystemUtil.getTotalMemory()));
        jvm.setMax(convertSize(SystemUtil.getMaxMemory()));
        jvm.setFree(convertSize(SystemUtil.getFreeMemory()));
        jvm.setThreadCount(SystemUtil.getTotalThreadCount());
        jvm.setVersion(jvmInfo.getVersion());
        jvm.setHome(SystemUtil.getJavaRuntimeInfo().getHomeDir());
    }

    /** 设置磁盘信息 */
    private void setSystemFiles() {
        List<OSFileStore> fsArray = OshiUtil.getOs().getFileSystem().getFileStores();
        for (OSFileStore fs : fsArray) {
            long free = fs.getUsableSpace();
            long total = fs.getTotalSpace();
            long used = total - free;
            SystemFile sysFile = new SystemFile();
            sysFile.setDirName(fs.getMount());
            sysFile.setSysTypeName(fs.getType());
            sysFile.setTypeName(fs.getName());
            sysFile.setTotal(convertSize(total));
            sysFile.setFree(convertSize(free));
            sysFile.setUsed(convertSize(used));
            systemFiles.add(sysFile);
        }
    }

    /**
     * 字节转换
     * @param size 字节大小
     * @return 转换后值
     */
    public String convertSize(long size) {
        long kb = 1024;
        long mb = kb * 1024;
        long gb = mb * 1024;
        if (size >= gb) {
            return String.format("%.1f GB", (float) size / gb);
        } else if (size >= mb) {
            float f = (float) size / mb;
            return String.format(f > 100 ? "%.0f MB" : "%.1f MB", f);
        } else if (size >= kb) {
            float f = (float) size / kb;
            return String.format(f > 100 ? "%.0f KB" : "%.1f KB", f);
        } else {
            return String.format("%d B", size);
        }
    }
}
